<template>
  <div id="recruit"></div>
</template>

<script>
export default {
  name: "college-recruit",
  props: ["year"],
  data() {
    return {
      url: {
        query: "/datacenter/student/recruit/college"
      },
      queryForm: {
        dialogFormVisible: false,
        data: {
          // 年份
          year: new Date().getFullYear().toString() 
        }
      }
    };
  },
  watch: {
    year: function(val, oldVal) {
      this.queryForm.data.year = val;
      this.drawLine();
    }
  },
  mounted() {
    this.drawLine();
  },
  methods: {
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById("recruit"));
      myChart.showLoading();
      // 加载某一年各个学院的招生图表
      this.$http.get(this.url.query, { params: this.queryForm.data }).then(
        (res) => {
          if (res.data.success) {
            let xAxisArray = new Array();
            let regNumList = new Array();
            let noRegNumList = new Array();
            let echartsData = res.data.data;
            echartsData.forEach(element => {
              xAxisArray.push(element.collegeName);
              regNumList.push(element.regNum);
              noRegNumList.push(element.noRegNum);
            });

            // 绘制图表
            myChart.setOption({
              title: { text: "" },
              tooltip: {
                trigger: "axis",
                axisPointer: {
                  // 坐标轴指示器，坐标轴触发有效
                  type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
                }
              },
              grid: {
                left: "5%",
                right: "5%",
                bottom: "25%"
              },
              legend: {
                data: ["报到人数", "未报到人数"]
              },
              xAxis: {
                type: "category",
                data: xAxisArray,
                axisTick: {
                  alignWithLabel: true
                },
                axisLabel: {
                  interval: 0,
                  rotate: 40
                }
              },
              yAxis: {},
              series: [
                {
                  name: "报到人数",
                  type: "bar",
                  stack: "人数",
                  data: regNumList
                },
                {
                  name: "未报到人数",
                  type: "bar",
                  stack: "人数",
                  data: noRegNumList
                }
              ]
            });
            myChart.hideLoading();
          } else {
            this.$message({
              type: "error",
              message: res.data.info
            });
          }
        },
        (res) => {
          this.$message({
            showClose: true,
            message: "网络错误" + res.status,
            type: "error"
          });
        }
      );
    }
  }
};
</script>


